# 餐厅发现器 - PowerShell 部署脚本
# 版本: 2.0
# 功能: 构建并部署应用到Docker容器

param(
    [string]$Environment = "Production",
    [switch]$SkipBuild = $false,
    [switch]$ViewLogs = $false
)

Write-Host "🚀 餐厅发现器部署脚本 v2.0" -ForegroundColor Green
Write-Host "=====================================" -ForegroundColor Green

# 设置错误处理
$ErrorActionPreference = "Stop"

try {
    # 1. 检查Docker
    Write-Host "🔍 检查Docker环境..." -ForegroundColor Yellow
    if (-not (Get-Command docker -ErrorAction SilentlyContinue)) {
        throw "Docker未安装或未在PATH中"
    }
    
    docker --version
    Write-Host "✅ Docker检查通过" -ForegroundColor Green

    # 2. 停止现有容器
    Write-Host "🛑 停止现有容器..." -ForegroundColor Yellow
    docker-compose down --remove-orphans 2>$null
    Write-Host "✅ 容器已停止" -ForegroundColor Green

    # 3. 构建应用
    if (-not $SkipBuild) {
        Write-Host "🔨 构建Docker镜像..." -ForegroundColor Yellow
        docker-compose build --no-cache
        Write-Host "✅ 镜像构建完成" -ForegroundColor Green
    } else {
        Write-Host "⏭️ 跳过构建步骤" -ForegroundColor Cyan
    }

    # 4. 启动服务
    Write-Host "🚀 启动服务..." -ForegroundColor Yellow
    docker-compose up -d
    
    # 5. 等待服务启动
    Write-Host "⏳ 等待服务启动..." -ForegroundColor Yellow
    Start-Sleep -Seconds 10
    
    # 6. 健康检查
    Write-Host "🏥 执行健康检查..." -ForegroundColor Yellow
    for ($i = 1; $i -le 12; $i++) {
        try {
            $response = Invoke-RestMethod -Uri "http://localhost:8080/health" -TimeoutSec 5
            Write-Host "✅ 应用健康检查通过" -ForegroundColor Green
            Write-Host "📊 状态: $($response.status)" -ForegroundColor Cyan
            break
        } catch {
            Write-Host "⏳ 等待应用启动... ($i/12)" -ForegroundColor Yellow
            Start-Sleep -Seconds 5
            if ($i -eq 12) {
                throw "应用启动超时"
            }
        }
    }

    # 7. 诊断检查
    Write-Host "🔧 执行系统诊断..." -ForegroundColor Yellow
    try {
        $diagnostics = Invoke-RestMethod -Uri "http://localhost:8080/api/diagnostics" -TimeoutSec 5
        Write-Host "📋 系统诊断结果:" -ForegroundColor Cyan
        Write-Host "   环境: $($diagnostics.environment)" -ForegroundColor White
        Write-Host "   高德地图: $(if ($diagnostics.gaodeMap.enabled) { '启用' } else { '禁用' })" -ForegroundColor White
        Write-Host "   API密钥: $(if ($diagnostics.gaodeMap.hasApiKey) { '已配置' } else { '未配置' })" -ForegroundColor White
        Write-Host "   状态: $($diagnostics.status)" -ForegroundColor White
    } catch {
        Write-Host "⚠️ 诊断检查失败，但应用可能仍然正常工作" -ForegroundColor Yellow
    }

    # 8. 显示访问信息
    Write-Host "" -ForegroundColor White
    Write-Host "🎉 部署成功！" -ForegroundColor Green
    Write-Host "🌐 访问地址: http://localhost:8080" -ForegroundColor Cyan
    Write-Host "🔧 诊断地址: http://localhost:8080/api/diagnostics" -ForegroundColor Cyan
    Write-Host "🏥 健康检查: http://localhost:8080/health" -ForegroundColor Cyan
    
    # 9. 可选查看日志
    if ($ViewLogs) {
        Write-Host "" -ForegroundColor White
        Write-Host "📋 实时日志:" -ForegroundColor Yellow
        docker-compose logs -f
    } else {
        Write-Host "" -ForegroundColor White
        Write-Host "💡 提示: 使用 './deploy.ps1 -ViewLogs' 查看实时日志" -ForegroundColor Cyan
        Write-Host "💡 提示: 使用 'docker-compose logs -f' 查看完整日志" -ForegroundColor Cyan
    }

} catch {
    Write-Host "" -ForegroundColor White
    Write-Host "❌ 部署失败: $($_.Exception.Message)" -ForegroundColor Red
    Write-Host "🔍 故障排除建议:" -ForegroundColor Yellow
    Write-Host "   1. 检查Docker是否正在运行" -ForegroundColor White
    Write-Host "   2. 检查端口8080是否被占用" -ForegroundColor White
    Write-Host "   3. 查看详细日志: docker-compose logs" -ForegroundColor White
    exit 1
} 